Image Processing
CIS 193 – Go Programming
Prakhar Bhandari, Adel Qalieh
CIS 193
Prakhar Bhandari, Adel Qalieh
CIS 193
Blank imports - remember these?
import _ "image/png"
package main import "image" import "image/color" import "image/png" import "os" func main() { img := image.NewRGBA(image.Rect(0, 0, 100, 100)) // Color the pixel at 5, 5 green img.Set(2, 3, color.RGBA{0, 250, 0, 255}) // Save to image.png f, _ := os.Create("image.png") defer f.Close() png.Encode(f, img) }
package main import "image" import "image/gif" import "image/png" import "os" func main() { files := []string{"img1.png", "img2.png","img3.png", "img2.png"} outGif := &gif.GIF{} for _, name := range files { f, _ := os.Open(name) img, _ := png.Decode(f) f.Close() outGif.Image = append(outGif.Image, img.(*image.Paletted)) outGif.Delay = append(outGif.Delay, 0) } f, _ := os.Create("output.gif") defer f.Close() gif.EncodeAll(f, outGif) }
The Go standard library is amazing, but it's often easier to use external packages
Inspecting your program for bugs, usually line by line. Other languages such as Java or Python have sophisticated debugging tools. What about Go?
So far, how have you been debugging?
main()
fmt.Println()
GDB is the GNU Debugger, which is intended for debugging C, C++, and Fortran programs. You can also use GDB with Go, but with many caveats. It also requires a UNIX-like environment, so Linux or MacOS. In other words, no Windows support.
See the documentation: golang.org/doc/gdb
gdb ./mygoexecutable
Warning: some operations and expressions will be substantially more verbose in GDB
A few other debugging tools have sprung up, currently the 2 favorites are delve and godebug.
$ go get github.com/mailgun/godebug
1. Insert a breakpoint anywhere in your code
_ = "breakpoint"
2. Start program with debugger
godebug run mygofile.go
Commands very similar to GDB:
No new homework, start working on final projects!